#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/11/27 14:08
# @Author : 闫峰
# @File    : category_kogan.py
import jsonpath
import requests
import os

import jsonpath
import requests

from common.setting import ensure_path_sep
from utils.logging_tool.log_control import LogHandler
from common.setting import ensure_path_sep
from utils.mysqlUtils.db_pool import Database
current_path = os.path.abspath(__file__)
current_dir = os.path.dirname(current_path)
path_list = current_path.split("\\")
dis_path = "\\".join(path_list[-2:-1])
INFO = LogHandler(ensure_path_sep(f"\\logs\\info-{dis_path}.log"), level='info')
ERROR = LogHandler(ensure_path_sep(f"\\logs\\error-{dis_path}.log"), level='error')
WARNING = LogHandler(ensure_path_sep(f'\\logs\\warning-{dis_path}.log'), level='warning')

class CategoryKogan:


    def get_category_list(self,ordersourceid):
        net_db = Database(ensure_path_sep("\\config\\znkd_mysql_config.yaml"), "znkd_database_prod")

        sql1 = """		SELECT DevIdSellerId , UserTokenSecretAccessKey  FROM sys_ordersource_authorization 
                WHERE CustomerId=1 AND OrderSourceId=%s;"""
        res1 = net_db.fetch_all(sql1, (ordersourceid,))
        UserTokenSecretAccessKey = res1[0]['UserTokenSecretAccessKey']
        DevIdSellerId = res1[0]['DevIdSellerId']
        headers = {
            "SellerToken": UserTokenSecretAccessKey,
            "SellerID": DevIdSellerId
        }
        # url = f"https://nimda.kogan.com/api/marketplace/v2/products/?created_after=2023-06-08&created_before=2023-06-07&size=500"
        url = f'https://nimda.kogan.com/api/marketplace/v2/category/?display_all=false&store_code=au&supplier_only=false'
        count = 0
        while True:
            res = requests.get(url=url, headers=headers, timeout=10)
            if res.status_code == 200:
                res = res.json()
            else:
                continue
            resluts = jsonpath.jsonpath(res, "$.body.results")[0]
            next = jsonpath.jsonpath(res, "$.body.next")[0]
            status = jsonpath.jsonpath(res, "$.status")[0]
            if next:
                if status == 'Complete':
                    url = next
                    category_list = resluts
                    count += len(category_list)
            else:
                break
        INFO.logger.info(f"ordersourceid={ordersourceid} 验证类目{count}个")


    def handler_reslutes(self,category_list):
        pass



if __name__ == '__main__':
    CategoryKogan().get_category_list(10779)